package com.atguigu.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.atguigu.commonutils.Response;
import com.atguigu.servicebase.handler.GuliException;
import com.atguigu.vod.Utils.ConstantPropertiesUtils;
import com.atguigu.vod.Utils.InitVodObject;
import com.atguigu.vod.service.VodService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import java.io.File;
import java.io.IOException;

/**
 * @Author zhaolie
 * @Date 2020/12/13 5:25 下午
 * @Version 1.0
 * @Description
 */

@RestController
@RequestMapping("/eduvod")
public class VodController {

    @Resource
    private VodService vodService;

    // 上传视频
    @PostMapping
    @ApiOperation("上传视频")
    public Response uploadVideo(MultipartFile file) {
        String videoId = null;
        try {
            videoId = vodService.uploadVideo(file);
            return Response.SUCCESS().data("videoId", videoId);
        } catch (IOException e) {
            e.printStackTrace();
            return Response.Failed();
        }
    }

    // 删除视频
    @DeleteMapping("/{id}")
    @ApiOperation("删除视频")
    public Response removeVideo(@PathVariable String id) {
        vodService.removeVideo(id);
        return Response.SUCCESS();
    }


    // 根据视频id获取视频凭证
    @GetMapping("getAuth/{id}")
    @ApiOperation("根据视频id获取视频凭证")
    private Response getAuth(@PathVariable String id) {
        try {
            DefaultAcsClient client =
                    InitVodObject.InitAcsObject(ConstantPropertiesUtils.ACCESS_KEY_ID, ConstantPropertiesUtils.ACCESS_KEY_SECRET);
            GetVideoPlayAuthRequest getVideoPlayAuthRequest = new GetVideoPlayAuthRequest();
            getVideoPlayAuthRequest.setVideoId(id);
            GetVideoPlayAuthResponse response = client.getAcsResponse(getVideoPlayAuthRequest);
            String playAuth = response.getPlayAuth();
            return Response.SUCCESS().data("playAuth", playAuth);
        } catch (ClientException e) {
            e.printStackTrace();
            throw new GuliException(20001, "获取凭证失败");
        }
    }

}
